package com.woniu.inlet.web.controller;

import com.woniu.inlet.web.fo.ConfirmSellOrder;
import com.woniu.inlet.web.fo.FixedInputBuyFo;
import com.woniu.inlet.web.vo.FindUserBuyOrderVo;
import com.woniu.inlet.web.vo.FindUserSellOrderVo;
import com.woniu.inlet.web.vo.FundHoldVo;
import com.woniu.inlet.web.vo.PayToOrderVo;
import com.woniu.service.CurrentService;
import com.woniu.service.FixedInputService;
import com.woniu.service.FundAndGoldService;
import com.woniu.service.OrderService;
import com.woniu.service.command.FindUserOrderCommand;
import com.woniu.service.command.FixedOrCurrentBuyCommand;
import com.woniu.service.command.FundOrGoldBuyCommand;
import com.woniu.service.command.UpdateBuyStatusCommand;
import com.woniu.utils.AdminPass;
import com.woniu.utils.JwtTokenUtil;
import com.woniu.utils.LoginPass;
import com.woniu.utils.ResultResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@Api(description = "对外暴露接口")
@Slf4j
@CrossOrigin
public class OrderController {

    @Autowired
    private CurrentService currentService;

    @Autowired
    private FixedInputService fixedInputService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private FundAndGoldService fundService;


    @PostMapping("/order/fixedInput/buy")
    @ApiOperation(value = "这是基金【-定投-】的购买方法-外部使用")
    public Object fixedInput(@RequestBody FixedInputBuyFo fo){
        String s = fixedInputService.buyFixed(fo);
        if(s.equals("n")){
            return new ResultResponseUtil<>(500,"ok","该基金不存在");
        }
        return new ResultResponseUtil<>(200,"ok","设置成功，将于下一轮为你进行买入，请确保余额充足");
    }

    @ApiOperation("根据userId，查询用户所有【买入】订单-外部使用")
    @ApiImplicitParam(name = "userId",value = "用户id",required = true)
    @GetMapping("/order/userOrder/buy")
    public Object getAllUserOrderBuy(Long userId){
        FindUserOrderCommand command=new FindUserOrderCommand(userId);
        Object execute = command.execute();
        if(execute.equals("invalid")){
            return new ResultResponseUtil<>(500,"ok","参数非法");
        }
        if(execute.equals("0")){
            return new ResultResponseUtil<>(500,"ok","没有数据");
        }
        return new ResultResponseUtil<>(execute);
    }

    @ApiOperation("根据userId，查询用户所有【卖出】订单-外部使用")
    @ApiImplicitParam(name = "userId",value = "用户id",required = true)
    @GetMapping("/order/userOrder/sell")
    public Object getAllUserOrderSell(Long userId){
        Object execute = orderService.findSellUserOrder(userId);
        if(execute.equals("invalid")){
            return new ResultResponseUtil<>(500,"ok","参数非法");
        }
        if(execute.equals("0")){
            return new ResultResponseUtil<>(500,"ok","没有数据");
        }
        return new ResultResponseUtil<>(execute);
    }

    @ApiOperation("根据userId和产品类型，查询用户所持有的产品-外部使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true),
            @ApiImplicitParam(name = "productType",value = "产品类型:fund/gold/fixed/current",required = true),
    })
    @GetMapping("/order/findUserAllHoldProductByType")
    public Object findAllUserHoldProductByType(Long userId,String productType){
        Object product = orderService.findAllUserHoldProductByType(userId, productType);
        if(product.equals("invalid")){
            return new ResultResponseUtil<>(500,"ok","参数非法");
        }
        if(product.equals("0")){
            return new ResultResponseUtil<>(500,"ok","没有数据");
        }
        return new ResultResponseUtil<>(product);
    }

    @ApiOperation("根据活期产品id，卖出拥有的活期产品-外部使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true),
            @ApiImplicitParam(name = "currentId",value = "活期产品id",required = true),
            @ApiImplicitParam(name = "payPassword",value = "支付密码",required = true)
    })
    @GetMapping("/order/current/sell")
    public Object sellCurrentProduct(String userId,Long currentId,String payPassword){
        Object s = currentService.sellCurrentProduct(userId, currentId, payPassword);
        if(s.equals("y")){
            return new ResultResponseUtil<>("正在为你卖出,请等待...");
        }
        return new ResultResponseUtil<>(500,"ok",s);
    }

    @ApiOperation("根据userId和codeId，查询用户当前基金或黄金的持仓详情-外部使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true),
            @ApiImplicitParam(name = "codeId",value = "基金代码",required = true),
    })
    @GetMapping("/order/findFundOrGoldHoldDetails")
    public Object fundHoldDetails(Long userId,String codeId){
        Object fundHoldVo = fundService.fundHoldDetails(userId, codeId);
        if(fundHoldVo.equals("invalid")){
            return new ResultResponseUtil<>(500,"ok","参数非法");
        }
        if(fundHoldVo.equals("0")){
            return new ResultResponseUtil<>(500,"ok","没有数据");
        }
        return new ResultResponseUtil<>(fundHoldVo);
    }


    @ApiOperation("（支付密码正确才可以）卖出基金或黄金-外部使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "登录token",required = true),
            @ApiImplicitParam(name = "userId",value = "用户id",required = true),
            @ApiImplicitParam(name = "payPassword",value = "支付密码",required = true),
            @ApiImplicitParam(name = "codeId",value = "基金代码",required = true),
            @ApiImplicitParam(name = "sellShare",value = "卖出的份额",required = true)
    })
    @LoginPass
    @AdminPass
    @GetMapping("/order/fundOrGold/sell")
    public Object sellFundOrGold(String token,String userId,String codeId,String sellShare,String payPassword){
        String s = fundService.sellFundOrGold(userId, codeId, sellShare,payPassword);
        if(s.equals("y")){
            return new ResultResponseUtil<>("正在为你卖出,请等待...");
        }
        return new ResultResponseUtil<>(500,"ok",s);
    }

    @ApiOperation("[管理员注解测试]-外部使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "登录token",required = true),
    })
    @AdminPass
    @GetMapping("/order/admin/test")
    public Object testAdmin(String token){
        return new ResultResponseUtil<>("欢迎你管理员");
    }


    @ApiOperation("[撤销'卖出']订单-外部使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sId",value = "卖出的订单id",required = true)
    })
    @GetMapping("/order/sellOrder/revocation")
    public Object revocationSellOrder(String sId){
        String s = fundService.revocationSellOrder(sId);
        if(s.equals("n")){
            return new ResultResponseUtil<>("撤销超时，不能撤销");
        }
        if(s.equals("invalid")){
            return new ResultResponseUtil<>(500,"ok","参数非法");
        }
        return new ResultResponseUtil<>("撤销进行中，请等待...");
    }

    @ApiOperation("[撤销'买入']订单-外部使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bId",value = "买入的订单id",required = true)
    })
    @GetMapping("/order/buyOrder/revocation")
    public Object revocationBuyOrder(String bId){
        String s = fundService.revocationBuyOrder(bId);
        if(s.equals("n")){
            return new ResultResponseUtil<>("撤销超时，不能撤销");
        }
        if(s.equals("invalid")){
            return new ResultResponseUtil<>(500,"ok","参数非法");
        }
        return new ResultResponseUtil<>("撤销进行中，请等待...");
    }

    @ApiOperation("得到有效的登录token，便于测试token-外部使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "用户名",required = true),
            @ApiImplicitParam(name = "role",value = "角色",required = true)
    })
    @GetMapping("/order/testToken/get")
    public Object revocationBuyOrder(String userName, String role){
        return JwtTokenUtil.createSign(userName,role);
    }



}
